package com.winchain.provider.expiration.rx;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.acct.transaction.service.AcctTxRecService;
import com.shell.bee.persistence.transaction.Transaction;
import com.shell.bee.persistence.transaction.TransactionUtil;
import com.winchain.basic.exception.ExtException;
import com.winchain.chainbill.chain.ChainCallbackService;
import java.util.List;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillExpTerminateSubscriber extends TransactionNewSubscriber<BillExpirationEvent> {
	@Autowired
	private AcctTxRecService acctTxaRecService;
	@Autowired
	private ChainCallbackService chainCallbackService;

	public void execute(BillExpirationEvent t) {
		String expDate = t.getDate();
		List<AcctTxRec> acctTxRecs = this.acctTxaRecService.selectExpInProcess(expDate);
		for (final AcctTxRec acctTxRec : acctTxRecs) {
			this.logger.info("��������������");
			try {
				TransactionUtil.execute(new Transaction() {
					public void doInTransaction() {
						AcctTxRec txRec = BillExpTerminateSubscriber.this.acctTxaRecService
								.selectAndLockByKey(acctTxRec.getTxId());
						if (!acctTxRec.getTxSts().equals(txRec.getTxSts())) {
							BillExpTerminateSubscriber.this.logger.warn("AcctTxRec txSts Has Changed.");
							return;
						}
						txRec.setTxSts("EX");
						BillExpTerminateSubscriber.this.acctTxaRecService.updateNotNull(txRec);
						BillExpTerminateSubscriber.this.chainCallbackService.callback(txRec, null);
					}
				}, new int[0]);
			} catch (Exception e) {
				e.printStackTrace();
				this.logger.error(getClass() + "��������", e);
				throw new ExtException(e.getMessage());
			}
		}
	}
}
